{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成器(generator,yield) & __next__(),send()\n",
    "生成器的作用：<p style=\"color:red\">一条一条输出，输出一个获取一个，不像return，输出后就马上返回，不能执行后面的函数。</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T09:16:49.311541Z",
     "start_time": "2020-08-26T09:16:49.301478Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "哇哈哈0\n",
      "哇哈哈1\n",
      "--------------------\n",
      "哇哈哈2\n",
      "哇哈哈3\n",
      "哇哈哈4\n",
      "哇哈哈5\n",
      "哇哈哈6\n",
      "哇哈哈7\n",
      "哇哈哈8\n",
      "哇哈哈9\n",
      "哇哈哈10\n",
      "哇哈哈11\n"
     ]
    }
   ],
   "source": [
    "def generator():\n",
    "    for i in range(200):\n",
    "        yield '哇哈哈%s' %i\n",
    "g = generator() #调用生成数函数，接受作用\n",
    "ret = g.__next__() #每执行一次g.__next__就是从生成器取值\n",
    "print(ret)\n",
    "ret = g.__next__() #每执行一次g.__next__就是从生成器取值\n",
    "print(ret)\n",
    "\n",
    "print(\"-\"*20)\n",
    "\n",
    "num = 0\n",
    "for i in g:\n",
    "    num += 1\n",
    "    if num > 10:\n",
    "        break\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "send 获取下一个值的效果和next基本一致，但是获取下一个值时，要给上一个yield的位置传递一个数据。\n",
    "\n",
    "第一次使用生成器时，是用next获取下一个值，最后一个yield不能接受外部的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T09:17:06.655526Z",
     "start_time": "2020-08-26T09:17:06.647329Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "*** 1\n",
      "====== hello\n",
      "456\n",
      "*** 2\n"
     ]
    }
   ],
   "source": [
    "#next send\n",
    "def generator():\n",
    "    print(123)\n",
    "    content = yield 1\n",
    "    print('======',content)\n",
    "    print(456)\n",
    "    yield 2 \n",
    "g = generator() #调用生成数函数，接受作用\n",
    "ret = g.__next__() #每执行一次g.__next__就是从生成器取值\n",
    "print('***',ret)\n",
    "ret = g.send('hello')  #send的效果和next一样\n",
    "print('***',ret)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最后一个yield如果想使用的话，解决办法如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T09:17:53.520020Z",
     "start_time": "2020-08-26T09:17:53.510149Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "*** 1\n",
      "====== hello\n",
      "456\n",
      "*** 2\n",
      "anlzou\n"
     ]
    }
   ],
   "source": [
    "def generator():\n",
    "    print(123)\n",
    "    content = yield 1\n",
    "    print('======',content)\n",
    "    print(456)\n",
    "    arg = yield 2\n",
    "    print(arg)\n",
    "    yield\n",
    "\n",
    "g = generator() #调用生成数函数，接受作用\n",
    "ret = g.__next__() #每执行一次g.__next__就是从生成器取值\n",
    "print('***',ret)\n",
    "ret = g.send('hello')  #send的效果和next一样\n",
    "print('***',ret)\n",
    "ret = g.send('anlzou')  #send的效果和next一样"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
